package com.supermap.dataservice.cloud.science.assets.order;

import com.supermap.dubbo.api.cloud.assets.order.Order;
import com.supermap.dubbo.api.cloud.assets.order.OrderComm;
import org.apache.ibatis.annotations.*;
import org.apache.logging.log4j.util.Strings;

import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {


    @InsertProvider(type = OrderMapperSQL.class,method = "insertcommSQL")
    Integer insert(@Param("list")List<Map<String,Object>> list);

    @Select("select * from t_scientific_purchase_commodity where puid=#{id} ")
    List<Map<String,Object>> getCommLIst(@Param("id") Integer id);
    @SelectProvider(type = OrderMapperSQL.class,method = "getOrderPageListSQL")
    List<Map<String,Object>> getOrderPageList(@Param("userName") String userName, @Param("title")String title, @Param("state")Integer state, @Param("bdate")String bdate, @Param("edate")String edate, @Param("page")Integer page, @Param("size")Integer size);

    @Select("select t1.*,t2.name,t2.stype,t2.conctent as scontent,t2.unit,t2.parenId from t_scientific_purchase_commodity t1\n" +
            "inner join t_scientific_product t2\n" +
            "on t1.pid=t2.id and del_flg=0 where t1.puid=#{id}")
    List<Map<String,Object>> getOrderComList(@Param("id")Integer id);
    @SelectProvider(type = OrderMapperSQL.class,method = "getOrderPageListCountSQL")
    Integer getOrderPageListCount(@Param("userName") String userName, @Param("title")String title, @Param("state")Integer state, @Param("bdate")String bdate, @Param("edate")String edate);


    @UpdateProvider(type = OrderMapperSQL.class,method = "manageAuditSQL")
    Integer manageAudit(@Param("id")Integer id,@Param("scontent")String scontent,@Param("suserId")String suserId, @Param("state")Integer state,@Param("sdata")String sdate);

    @Select("select last_insert_id()")
    Integer getId();

    @Insert("insert into t_scientific_purchase(title,content,suserId,supplier,sPhone,budgetMoney,realityMoney)" +
            " values(#{order.title},#{order.content},#{order.suserId},#{order.supplier},#{order.sphone},#{order.budgetMoney},#{order.realityMoney})")
    Integer putOrder(@Param("order")Order order);



    @Insert("insert into t_scientific_purchase_commodity(puid,pid,price,sprice,num,content) " +
            " values(#{orderComm.puid},#{orderComm.pid},#{orderComm.price},#{orderComm.sprice},#{orderComm.num},#{orderComm.content})")
    Integer putOrdercomm(@Param("orderComm")OrderComm orderComm);

    @Update("update t_scientific_purchase_commodity set pid=#{orderComm.pid},price=#{orderComm.price},sprice=#{orderComm.sprice},num=#{orderComm.num},content=#{orderComm.content} where puid=#{orderComm.puid}")
    Integer postOrderConmm(@Param("orderComm") OrderComm orderComm);


    @Delete("delete from t_scientific_purchase_commodity where puid=#{id}")
    void  del1(@Param("id")Integer id);
    @Delete("delete from t_scientific_purchase where id=#{id}")
    void  del2(@Param("id")Integer id);

    class  OrderMapperSQL{
        public String insertcommSQL(Map map) {
            StringBuilder stringBuilder=new StringBuilder();
            List<Map<String,Object>> list=(List<Map<String,Object>>)map.get("list");
            stringBuilder.append("insert into t_scientific_commodity(pid,oid) values ");
            list.stream().forEach(obj->{
                Integer integer=(Integer)obj.get("num");
                for(int i=0;i<integer;i++){
                    stringBuilder.append(" ("+obj.get("pid")+","+obj.get("puid")+"),");
                }

            });

            return  stringBuilder.substring(0,stringBuilder.length()-1);

        }
        public String manageAuditSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            Integer id=(Integer)map.get("id");
            String scontent=(String)map.get("scontent");
            String suserId=(String)map.get("suserId");
            Integer state=(Integer)map.get("state");
            String sdate=(String)map.get("sdata");
            if(state==1){
                stringBuilder.append("update t_scientific_purchase set scontent='"+scontent+"',cuserId='"+suserId+"',sdate='"+sdate+"',state="+state+" where id="+id+"");
            }
            if(state==2){
                stringBuilder.append("update t_scientific_purchase set scontent='"+scontent+"',cuserId='"+suserId+"',cgdate='"+sdate+"',sdate='"+sdate+"',state="+state+" where id="+id+"");
            }
            if(state==3){
                stringBuilder.append("update t_scientific_purchase set scontent='"+scontent+"',cuserId='"+suserId+"',edate='"+sdate+"',sdate='"+sdate+"',state="+state+" where id="+id+"");
            }
            return  stringBuilder.toString();
        }
        public String getOrderPageListCountSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            String userName =(String)map.get("userName");
            String title =(String)map.get("title");
            Integer state =(Integer)map.get("state");
            String bdate =(String)map.get("bdate");
            String edate =(String)map.get("edate");
            stringBuilder.append("select count(1)" +
                    "from cagmpisards.t_scientific_purchase t1 \n" +
                    "inner join cqpamiss.t_s_base_user t2\n" +
                    "on t1.suserId=t2.ID\n" +
                    "where 1=1  ");

            if(Strings.isNotBlank(userName)){
                stringBuilder.append(" and t2.username like concat('%','"+userName+"','%') ");
            }
            if(Strings.isNotBlank(title)){
                stringBuilder.append(" and t1.title like concat('%','"+title+"','%') ");
            }
            if(state!=null){
                stringBuilder.append(" and t1.state="+state+" ");
            }
            if(Strings.isNotBlank(bdate)&&Strings.isNotBlank(edate)){
                stringBuilder.append("  and t1.cgdate BETWEEN '"+bdate+"' and '"+edate+"' ");
            }
            return  stringBuilder.toString();
        }
        public String getOrderPageListSQL(Map map){
            StringBuilder stringBuilder=new StringBuilder();
            String userName =(String)map.get("userName");
            String title =(String)map.get("title");
            Integer state =(Integer)map.get("state");
            String bdate =(String)map.get("bdate");
            String edate =(String)map.get("edate");
            Integer page =(Integer)map.get("page");
            Integer size =(Integer)map.get("size");
            page=(page-1)*size;
            stringBuilder.append("select t1.*, \n" +
                    "t2.realname as  cname,\n" +
                    "(select realname from cqpamiss.t_s_base_user where id=t1.suserId) as sname\n" +
                    "from cagmpisards.t_scientific_purchase t1 \n" +
                    "inner join cqpamiss.t_s_base_user t2\n" +
                    "on t1.suserId=t2.ID\n" +
                    "where 1=1  ");

            if(Strings.isNotBlank(userName)){
                stringBuilder.append(" and t2.username like concat('%','"+userName+"','%') ");
            }
            if(Strings.isNotBlank(title)){
                stringBuilder.append(" and t1.title like concat('%','"+title+"','%') ");
            }
            if(state!=null){
                stringBuilder.append(" and t1.state="+state+" ");
            }
            if(Strings.isNotBlank(bdate)&&Strings.isNotBlank(edate)){
                stringBuilder.append("  and t1.cgdate BETWEEN '"+bdate+"' and '"+edate+"' ");
            }
            stringBuilder.append(" limit "+page+","+size+" ");
            return  stringBuilder.toString();
        }
    }

}
